/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.controller.dialog;

import cn.ac.nercis.pes.community.component.controls.PesPasswordField;
import cn.ac.nercis.pes.community.component.dialog.MessageDialog;
import cn.ac.nercis.pes.community.component.dialog.MessageType;
import cn.ac.nercis.pes.community.component.dialog.PageDialog;
import cn.ac.nercis.pes.community.support.FXMLController;
import cn.ac.nercis.pes.community.utils.AppStore;
import cn.ac.nercis.pes.model.dto.manage.ModifyPwdDTO;
import cn.ac.nercis.pes.service.manage.UserService;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.CompletableFuture;

/**
 * 修改密码弹框控制器
 * @author zpy
 * @version 1.0.0
 */
@FXMLController
public class ModifyPwdController implements Initializable {
    @FXML
    private PesPasswordField sourcePwdTxt;
    @FXML
    private PesPasswordField newPwdTxt;
    @FXML
    private PesPasswordField confirmPwdTxt;

    private final UserService userService;

    private PageDialog parentDialog;

    @Autowired
    public ModifyPwdController(UserService userService) {
        this.userService = userService;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        confirmPwdTxt.setFirstPasswordField(newPwdTxt);
    }

    public void initData(PageDialog dialog){
        this.parentDialog = dialog;
        this.sourcePwdTxt.setText("");
        this.newPwdTxt.setText("");
        this.confirmPwdTxt.setText("");
        this.sourcePwdTxt.resetValidation();
        this.newPwdTxt.resetValidation();
        this.confirmPwdTxt.resetValidation();
    }

    public void onSaveClick(){
        if(!sourcePwdTxt.validate() || !newPwdTxt.validate() || !confirmPwdTxt.validate()){
            return;
        }
        ModifyPwdDTO modifyPwdDTO = ModifyPwdDTO.builder()
                .id(AppStore.getCurrentUser().getId())
                .oldPwd(sourcePwdTxt.getText())
                .newPwd(newPwdTxt.getText())
                .build();
        CompletableFuture.supplyAsync(()->userService.modifyPassword(modifyPwdDTO))
                .thenAccept(res-> Platform.runLater(()->{
                    if(res.isSuccess()){
                        this.parentDialog.close();
                        MessageDialog.builder()
                                .withType(MessageType.SUCCESS).withMessage("密码修改成功")
                                .build().show();
                    }else{
                        MessageDialog.builder()
                                .withType(MessageType.FAIL).withMessage(res.getMsg())
                                .build().show();
                    }
                }));
    }
}